//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

import { type Channel, invoke } from "@tauri-apps/api/core";

export type FS_OWNER_TYPE = number;
export const FS_OWNER_TYPE_USER: FS_OWNER_TYPE = 0;
// export const FS_OWNER_TYPE_TEAM: FS_OWNER_TYPE = 1;
export const FS_OWNER_TYPE_GLOBAL: FS_OWNER_TYPE = 2;
export const FS_OWNER_TYPE_BOARD: FS_OWNER_TYPE = 3;

export type FILE_OWNER_TYPE = number;
export const FILE_OWNER_TYPE_NONE: FILE_OWNER_TYPE = 0;        //未设置owner
export const FILE_OWNER_TYPE_USER_PHOTO: FILE_OWNER_TYPE = 1;  //用户头像
// export const FILE_OWNER_TYPE_TEAM_PHOTO: FILE_OWNER_TYPE = 2;  //团队头像
export const FILE_OWNER_TYPE_BOARD_IMAGE: FILE_OWNER_TYPE = 3; //画板中的图片
// export const FILE_OWNER_TYPE_TEAM_DISK_FILE: FILE_OWNER_TYPE = 4; //团队磁盘文件

export type DownloadResult = {
    exist_in_local: boolean;
    local_path: string;
    local_dir: string;
};

export type FsProgressEvent = {
    total_step: number;
    cur_step: number;
    file_id: string;
    file_size: number;
};

export type FsStatus = {
    fs_id: string;
    owner_type: FS_OWNER_TYPE;
    owner_id: string;
    file_count: number;
    total_file_size: number;
    max_filecount: number;
    max_total_size: number;
    last_gc_time: number;
};

export type WriteFileResponse = {
    code: number;
    err_msg: string;
    file_id: string;
};

export type SetFileOwnerRequest = {
    session_id: string;
    fs_id: string;
    file_id: string;
    owner_type: FILE_OWNER_TYPE;
    owner_id: string;
};



export type SetFileOwnerResponse = {
    code: number;
    err_msg: string;
};


export type GetFsStatusRequest = {
    session_id: string;
    fs_id: string;
};



export type GetFsStatusResponse = {
    code: number;
    err_msg: string;
    fs_status: FsStatus;
};

export async function stat_local_file(file_path: string): Promise<number> {
    return invoke<number>('fs_api_cmds_stat_local_file', { filePath: file_path });
}

export async function get_cache_file(fs_id: string, file_id: string, file_name: string): Promise<DownloadResult> {
    return invoke<DownloadResult>('fs_api_cmds_get_cache_file', {
        fsId: fs_id,
        fileId: file_id,
        fileName: file_name,
    });
}

export async function download_file(
    session_id: string,
    fs_id: string,
    file_id: string,
    as_name: string,
    on_event: Channel<FsProgressEvent>,
): Promise<DownloadResult> {
    return invoke<DownloadResult>('fs_api_cmds_download_file', {
        sessionId: session_id,
        fsId: fs_id,
        fileId: file_id,
        asName: as_name,
        onEvent: on_event,
    });
}

export async function write_file(
    session_id: string,
    fs_id: string,
    file_name: string,
    on_event: Channel<FsProgressEvent>): Promise<WriteFileResponse> {
    return invoke<WriteFileResponse>('fs_api_cmds_write_file', {
        sessionId: session_id,
        fsId: fs_id,
        fileName: file_name,
        onEvent: on_event,
    })
}

export async function get_file_name(file_path: string): Promise<string> {
    return invoke<string>('fs_api_cmds_get_file_name', { filePath: file_path });
}

export async function write_thumb_image_file(
    session_id: string,
    fs_id: string,
    file_path: string,
    width: number,
    height: number,
    on_event: Channel<FsProgressEvent>): Promise<WriteFileResponse> {
    return invoke<WriteFileResponse>('fs_api_cmds_write_thumb_image_file', {
        sessionId: session_id,
        fsId: fs_id,
        filePath: file_path,
        width: width,
        height: height,
        onEvent: on_event,
    })
}

export async function save_tmp_file_base64(file_name: string, data: string): Promise<string> {
    return invoke<string>('fs_api_cmds_save_tmp_file_base64', {
        fileName: file_name,
        data: data,
    });
}

export async function set_file_owner(request: SetFileOwnerRequest): Promise<SetFileOwnerResponse> {
    const cmd = "fs_api_cmds_set_file_owner";
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<SetFileOwnerResponse>(cmd, {
        request: request,
    })
}

//获取单个文件系统信息
export async function get_fs_status(request: GetFsStatusRequest): Promise<GetFsStatusResponse> {
    const cmd = "fs_api_cmds_get_fs_status";
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<GetFsStatusResponse>(cmd, {
        request: request,
    })
}

export async function make_tmp_dir(): Promise<string> {
    return invoke<string>("fs_api_cmds_make_tmp_dir", {});
}